package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.PeachBlossomApi;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.PicUploadResult;
import com.tanhua.server.vo.SoundUploadResult;
import com.tanhua.server.vo.SoundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.TimeZone;
import java.util.concurrent.TimeUnit;


/**
 * @Author: cloudy
 * @Description:
 * @Date: Created in 9:10 2021/1/17
 * @ModifiedBy:
 */
@Service
public class PeachBlossomService {

    @Autowired
    private SoundUploadService soundUploadService;

    @Reference(version = "1.0.0")
    private PeachBlossomApi peachBlossomApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    public Boolean sendMsg(MultipartFile soundFile) {

        if (soundFile == null) {
            return false;
        }

        User user = UserThreadLocal.get();
        SoundUploadResult soundUploadResult = this.soundUploadService.upload(soundFile);
        if (soundUploadResult.getStatus() == null) {
            return false;
        }

        Sound sound = new Sound();
        sound.setUserId(user.getId());
        sound.setSoundUrl(soundUploadResult.getName());

        return this.peachBlossomApi.sendMsg(sound);
    }

    public SoundVo getMsg() {
        SoundVo soundVo = new SoundVo();

        String redisKey = "SOUND_TIME_OUT_" + UserThreadLocal.get().getId();
        //判断redis中有无缓存  没有就创建一个默认值为10  默认销毁时间为24小时
        if (!this.redisTemplate.hasKey(redisKey)) {
            long current = System.currentTimeMillis();
            long theDay = current - (current + TimeZone.getDefault().getRawOffset()) % (1000 * 3600 * 24) + 1000 * 3600 * 24;
            this.redisTemplate.opsForValue().set(redisKey, "10", theDay - current, TimeUnit.MILLISECONDS);
        }
        //走到这里,就是有缓存,我们就判断当前值是否等于0,等于0直接返回一个remainingTimes = 0
        if ("0".equals(this.redisTemplate.opsForValue().get(redisKey))) {
            soundVo.setRemainingTimes(0);
            return soundVo;
        }
        //否则将值减去1, 并进行
        this.redisTemplate.opsForValue().decrement(redisKey);

        Long id = UserThreadLocal.get().getId();
        Sound sound = this.peachBlossomApi.getMsg(id);

        if (sound == null) {
            return null;
        }
        Long userId = sound.getUserId();
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(userId);

        soundVo.setId(userId.intValue());
        soundVo.setAge(userInfo.getAge());
        soundVo.setAvatar(userInfo.getLogo());
        soundVo.setGender(userInfo.getSex().name().toLowerCase());
        soundVo.setNickname(userInfo.getNickName());
        soundVo.setSoundUrl(sound.getSoundUrl());
        soundVo.setRemainingTimes(Integer.parseInt(this.redisTemplate.opsForValue().get(redisKey)));

        return soundVo;
    }
}
